<!-- LayoutHeader -->
<template>
  <div class="LayoutHeader" onselectstart="return false;" style="-moz-user-select:none;">
    <div class="partLeft">
      <i
        :class="[
          isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold',
          'asideBtn'
        ]"
        @click="toggleCollapse"
      ></i>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item
          v-for="item in CurrentPage.breadcrumbs"
          :key="item.path"
          :to="{ path: item.path }"
        >{{ item.meta.title }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="partRight">
      <!-- 全屏开关 -->
      <svg-icon
        :iconName="isFullscreen ? 'suoxiao' : 'fangda'"
        w="20px"
        h="20px"
        margin="0 20px"
        color="#CCC"
        @iconClick="pageScreenfull"
      />
      <!-- pageBar 开关 -->
      <el-tooltip class="item" effect="dark" content="页面栏开关" placement="bottom-start">
        <i
          :class="[pageBarShow ? 'action' : '', 'PageBarBtn', 'el-icon-guide']"
          @click="PageBarCollapse"
        ></i>
      </el-tooltip>
      <!-- 用户头像 -->
      <el-dropdown @command="userDropdown">
        <el-avatar
          shape="square"
          size="medium"
          src="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
        ></el-avatar>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="toHome">个人主页</el-dropdown-item>
          <el-dropdown-item command>狮子头</el-dropdown-item>
          <el-dropdown-item command>螺蛳粉</el-dropdown-item>
          <el-dropdown-item command="Logout" divided>
            <i class="el-icon-warning-outline"></i>退出
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import screenfull from "screenfull";
export default {
  name: "LayoutHeader",
  components: {},
  data () {
    return {
      isFullscreen: false
    };
  },
  computed: {
    ...mapState("Layout", ["isCollapse", "CurrentPage", "pageBarShow"]),
    ...mapState("user", ["userName"])
  },
  watch: {},
  //生命周期 - 创建完成（可以访问当前this实例）
  created () { },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    window.onresize = () => {
      // 全屏下监控是否按键了ESC
      if (!this.checkFull()) {
        // 全屏下按键esc后要执行的动作
        this.isFullscreen = false;
      }
    };
  },
  methods: {
    ...mapMutations("Layout", ["toggleCollapse", "PageBarCollapse"]),
    userDropdown (command) {
      switch (command) {
        case "toHome":
          this.$router.push("/dashbord");
          break;
        case "Logout":
          this.logout();
          break;
      }
    },
    logout () {
      // 清空token
      window.sessionStorage.clear();
      // 直接刷新页面重置VUEX
      window.location.reload();
      this.$router.push("/login");
    },
    /**
     * 全屏事件
     */
    pageScreenfull () {
      if (!screenfull.isEnabled) {
        this.$message({
          message: "您的浏览器不支持全屏展示！",
          type: "warning"
        });
        return false;
      }
      screenfull.toggle();
      this.isFullscreen = !this.isFullscreen;
    },
    /**
     * 是否全屏并按键ESC键的方法
     */
    checkFull () {
      var isFull =
        document.fullscreenEnabled ||
        window.fullScreen ||
        document.webkitIsFullScreen ||
        document.msFullscreenEnabled;
      if (isFull === undefined) {
        isFull = false;
      }
      return isFull;
    }
  },
  beforeCreate () { }, //生命周期 - 创建之前
  beforeMount () { }, //生命周期 - 挂载之前
  beforeUpdate () { }, //生命周期 - 更新之前
  updated () { }, //生命周期 - 更新之后
  beforeDestroy () { }, //生命周期 - 销毁之前
  destroyed () { }, //生命周期 - 销毁完成
  activated () { } //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="scss" scoped>
.LayoutHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 54px;
  color: $ThemeText;

  .partLeft {
    display: flex;
    align-items: center;
    .asideBtn {
      color: $ThemeText_grey;
      font-size: 30px;
    }
    .el-breadcrumb {
      margin-left: 15px;

      /deep/ .el-breadcrumb__inner {
        color: #ccc !important;
      }
    }
  }
  .partRight {
    display: flex;
    align-items: center;
    justify-content: end;
    .PageBarBtn {
      font-size: 22px;
      margin-right: 20px;
      &.action {
        color: $ThemeActiveText;
      }
    }
    .userInfo {
      display: flex;
      align-items: center;
      color: $ThemeActiveText;
      font-size: 26px;
      font-weight: bold;
    }
  }
}
</style>
